package com.kaili.controller.web.v1;

import com.baomidou.mybatisplus.plugins.Page;
import com.kaili.common.exception.BaseExcept;
import com.kaili.db.service.IHsOptEwbNoticeService;
import com.kaili.db.service.IHsOptEwbService;
import com.kaili.except.ResponseStatus;
import com.kaili.pojo.dto.notice.NoticeAddDTO;
import com.kaili.pojo.dto.notice.NoticeQueryDTO;
import com.kaili.pojo.entity.HsOptEwb;
import com.kaili.pojo.vo.EwbNoticeVO;
import com.kaili.version.ApiVersion;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @AUTHOR pengdongxu.
 * @Date: 18-5-21 下午5:06.
 * @COPYRIGHT 杭州凯立
 */
@Api(tags = "到件通知", description = "到件通知API")
@RestController
@ApiVersion(1)
@Validated
@RequestMapping("/{api_version}")
public class EwbNoticeController {

    @Autowired
    private IHsOptEwbNoticeService hsOptEwbNoticeService;
    @Autowired
    private IHsOptEwbService hsOptEwbService;

    @ApiOperation(value = "新增到货通知")
    @PostMapping("/notices")
    public ResponseStatus createdEwbNotice(@Validated @RequestBody NoticeAddDTO req){
        //校验改单号是否录单、签收
        HsOptEwb ewb = hsOptEwbService.getEWbByEwbNo(req.getEwbNo());
        if (ewb == null || ewb.getIsSign() == 1){
            return ResponseStatus.EXCEPTION_ADD_NONE;
        }
        hsOptEwbNoticeService.modifiedNotice(req.getEwbNo());
        if (0 == hsOptEwbNoticeService.createdEwbNotice(req)) {
            return ResponseStatus.RESPONSE_OK;
        } else {
            throw new BaseExcept(ResponseStatus.TRANS_SEND_ADD_ERR);
        }
    }

    @ApiOperation(value = "查询到货通知")
    @GetMapping("/notices")
    public Page<EwbNoticeVO> queryEwbAppointList(@Validated @ModelAttribute NoticeQueryDTO req){
        if (req.getCur() != null && req.getPageSize() != null){
            Page<EwbNoticeVO> page = new Page<>(req.getCur(),req.getPageSize());
            Map<Object, Object> search = new HashMap<>();
            search.putAll(new org.apache.commons.beanutils.BeanMap(req));
            page = hsOptEwbNoticeService.queryEwbNoticeList(page ,search);
            return page;
        }
        throw  new BaseExcept(ResponseStatus.NOTICE_QUERY_ERR);
    }
}
